// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Create a enemy convoy
// -----------------------------------------------
if (!isServer) exitWith {};

private [
	"_arrayofpilot",
	"_arrayofvehicle",
	"_globalgroup",
	"_group",
	"_kindofarmedvehicles",
	"_kindofsupplyvehicles",
	"_numberofvehicle",
	"_position",
	"_target",
	"_vehicle"
];

_kindofarmedvehicles  = _this select 0;
_kindofsupplyvehicles = _this select 1;
_numberofvehicle      = _this select 2;

if (isNil "_numberofvehicle") then {
	_numberofvehicle = 1;
};

_target = wctownlocations call BIS_fnc_selectRandom;
_position = (position _target) findEmptyPosition [10, 500];

if (_position select 0 == 0) exitWith {
	diag_log "WARCONTEXT: CANNOT FIND EMPTY POSITION FOR CONVOY SPAWN";
};

_globalgroup = createGroup east;

for "_i" from 1 to _numberofvehicle do {
	if ((_numberofvehicle > 1) and (random 1> 0.5) and (_i > 1)) then {
		_arrayofvehicle = [_position, 0, (_kindofsupplyvehicles call BIS_fnc_selectRandom), east] call BIS_fnc_spawnVehicle;
	} else {
		_arrayofvehicle = [_position, 0, (_kindofarmedvehicles call BIS_fnc_selectRandom), east] call BIS_fnc_spawnVehicle;
	};

	_vehicle      = _arrayofvehicle select 0;
	_arrayofpilot = _arrayofvehicle select 1;
	_group        = _arrayofvehicle select 2;

	_vehicle setVehicleLock "LOCKED";
	_vehicle setVariable ["cible", objNull, false];

	wcgarbage = [_vehicle] spawn WC_fnc_vehiclehandler;

	_arrayofpilot joinSilent _globalgroup;
	deleteGroup _group;
	sleep 1;
};

wcgarbage = [_globalgroup] spawn WC_fnc_grouphandler;
wcgarbage = [_globalgroup] spawn WC_fnc_roadpatrol;